package com.app.common.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.css.sword.kernel.base.exception.SwordBaseCheckedException;
import com.css.sword.kernel.utils.SwordCacheUtils;





public class CacheUtil {
	
	/**
	 * 直接从数据库中获取下拉树数据
	 * @param code：表中代码字段
	 * @param pcode：表中父节点字段
	 * @param caption：表中代码名称字段
	 * @param tableName：表名
	 * @param dao：数据库连接
	 * @return
	 * @throws SwordBaseCheckedException
	 */
	/*public static List getDataFromDB(String tableName,IPersistenceService dao,String code,String pcode,String caption) throws SwordBaseCheckedException{
		List result = new ArrayList();
		if(null == code)
			code = "id";
		if(null == pcode)
			code = "pid";
		if(null == caption)
			code = "mc";
		String sql = "select "+code +" as code" +","+pcode+" as pcode"+","+caption+" as caption " +"from  "+ tableName;
		result = dao.queryMapListBySql("pulltree", sql, new String[]{tableName}, null);	
		return result;
	}*/
	
	/**
	 * 直接从缓存中获取下拉树或下拉列表数据
	 * @param tableName：表名
	 * @return
	 * @throws SwordBaseCheckedException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List getPullData(String tableName) throws SwordBaseCheckedException{
		List result = new ArrayList();
		Collection cache = SwordCacheUtils.getAllDataFromKV(tableName);
		if(null != cache){
			Iterator it = cache.iterator();
			while(it.hasNext()){
				Map map = (Map)it.next();
				Map temp = new HashMap();
			    Iterator keyIt = map.keySet().iterator();
			    while(keyIt.hasNext()){
			    	String key = (String)keyIt.next();
			    	String value = "";
			    	if ((map.get(key) instanceof Integer) ||(map.get(key) instanceof Long) ) {
						value = String.valueOf(map.get(key));
					}else {
				    	value = (String)map.get(key);
					}
			    	temp.put(key.toLowerCase(), value);
			    }
				result.add(temp);
				
			}
		}
		return result;
	}
	
	/**
	 * 直接从缓存中查询数据
	 * @param tableName：表名
	 * @param key：字段名（不区分大小写）
	 * @param value：字段值（区分大小写）
	 * @return
	 * @throws SwordBaseCheckedException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map getCacheData(String tableName,String key,String value) throws SwordBaseCheckedException{
		//List result = new ArrayList();
		Map result = new HashMap();
		Collection cache = SwordCacheUtils.getAllDataFromKV(tableName);
		if(null != cache){
			Iterator it = cache.iterator();
			boolean flag = false;
			while(it.hasNext()){
				Map map = (Map)it.next();
				Map temp = new HashMap();
				Iterator keyIt = map.keySet().iterator();
				while(keyIt.hasNext()){
					String tmpkey = (String)keyIt.next();
					String tmpvalue = String.valueOf(map.get(tmpkey));
					if (tmpkey.toLowerCase().equals(key.toLowerCase())&&tmpvalue.equals(value)) {
						flag=true;
					}
					temp.put(tmpkey.toLowerCase(), tmpvalue);
				}
				if(flag){//只有当key、value匹配时才加入范围结果
					return temp;
				}
			}
		}
		return result;
	}
	


}
